﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class TLNhapKho : System.Web.UI.Page
{
    int pc;
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmNhapKho";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 6;
    static int CurrentPage = 1;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;
                    btnCancel_Click(sender, e);
                    Title = "Quản lý nhập kho | " + Header;


                    LoadNCC_TimKiem();
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThang.Items.Insert(0, new ListItem("Theo thời gian", ""));
                    dlThang.SelectedIndex = 0;

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNam.SelectedValue = year2.ToString();

                    txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dlNam.Visible = false;
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        FirstGridViewRow();
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_xdanhapkho_List", p);
        GV.DataSource = dt;
        GV.DataBind();
       
    }
    
   
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    

    void LoadNCC_TimKiem()
    {
        dropNhanVien.Items.Clear();
        dropNhanVien.DataSource = ex.GetData("sp_xdanhapkho_ListNhaCungCap_Drop");
        dropNhanVien.DataBind();
        dropNhanVien.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", ""));
        dropNhanVien.SelectedIndex = 0;
    }
    
    
    
    string ConvertDecimal(double s)
    {
        string q = "";
        if (s.ToString().Contains("."))
        {
            double x = double.Parse(s.ToString().Substring(0, s.ToString().IndexOf('.')));
            q = (x != 0 && x > 999) ? string.Format("{0:0,0}", x) : x.ToString();
            q += s.ToString().Substring(s.ToString().IndexOf('.'));
        }
        else
            q = (s != 0 && s > 999) ? string.Format("{0:0,0}", s) : s.ToString();
        return q;
    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtngaythang = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtngaythang");
                    DropDownList dlnhacungcap = (DropDownList)GV1.Rows[rowIndex].Cells[2].FindControl("drnhacungcap");
                    DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[3].FindControl("drnhomvatlieu");
                    DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[4].FindControl("drloaivatlieu");
                    DropDownList dldonvi = (DropDownList)GV1.Rows[rowIndex].Cells[5].FindControl("dldonvi");
                    TextBox txtsoluong = (TextBox)GV1.Rows[rowIndex].Cells[6].FindControl("txtsoluong");
                    TextBox txtdongia = (TextBox)GV1.Rows[rowIndex].Cells[7].FindControl("txtdongia");
                    TextBox txtthanhtienhang = (TextBox)GV1.Rows[rowIndex].Cells[8].FindControl("txtthanhtienhang");
                    TextBox txtthanhtien = (TextBox)GV1.Rows[rowIndex].Cells[9].FindControl("txtthanhtien");
                    TextBox txtmathietbi = (TextBox)GV1.Rows[rowIndex].Cells[10].FindControl("txtmathietbi");
                    CheckBox ckVAT = (CheckBox)GV1.Rows[rowIndex].Cells[11].FindControl("ckVAT");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["NgayThang"] = txtngaythang.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["DonVi"] = dldonvi.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["SoLuong"] = txtsoluong.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["DonGia"] = txtdongia.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["ThanhTien"] = txtthanhtien.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["CoVAT"] = ckVAT.Checked.ToString();    
                    dtCurrentTable.Rows[i - 1]["NhaCungCap"] = dlnhacungcap.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["NhomVatLieu"] = dlnhomvatlieu.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["LoaiVatLieu"] = dlloaivatlieu.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["MaThietBi"] = txtmathietbi.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["ThanhTienHang"] = txtthanhtienhang.Text.Trim();
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                GV1.DataSource = dtCurrentTable;
                GV1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();

    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox txtngaythang = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtngaythang");
                    DropDownList dlnhacungcap = (DropDownList)GV1.Rows[rowIndex].Cells[2].FindControl("drnhacungcap");
                    DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[3].FindControl("drnhomvatlieu");
                    DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[4].FindControl("drloaivatlieu");
                    DropDownList dldonvi = (DropDownList)GV1.Rows[rowIndex].Cells[5].FindControl("dldonvi");
                    TextBox txtsoluong = (TextBox)GV1.Rows[rowIndex].Cells[6].FindControl("txtsoluong");
                    TextBox txtdongia = (TextBox)GV1.Rows[rowIndex].Cells[7].FindControl("txtdongia");
                    TextBox txtthanhtienhang = (TextBox)GV1.Rows[rowIndex].Cells[8].FindControl("txtthanhtienhang");
                    TextBox txtthanhtien = (TextBox)GV1.Rows[rowIndex].Cells[9].FindControl("txtthanhtien");
                    TextBox txtmathietbi = (TextBox)GV1.Rows[rowIndex].Cells[10].FindControl("txtmathietbi");
                    CheckBox ckVAT = (CheckBox)GV1.Rows[rowIndex].Cells[11].FindControl("ckVAT");
                    
                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    txtngaythang.Focus();
                    txtngaythang.Text = dt.Rows[i]["NgayThang"].ToString();
                   
                    dlnhacungcap.SelectedValue = dt.Rows[i]["NhaCungCap"].ToString();
                    dlnhomvatlieu.SelectedValue = dt.Rows[i]["NhomVatLieu"].ToString();
                    dlloaivatlieu.SelectedValue = dt.Rows[i]["LoaiVatLieu"].ToString();
                    dldonvi.SelectedValue = dt.Rows[i]["DonVi"].ToString();
                    txtsoluong.Text = dt.Rows[i]["SoLuong"].ToString();
                    txtdongia.Text = dt.Rows[i]["DonGia"].ToString();
                    txtmathietbi.Text = dt.Rows[i]["MaThietBi"].ToString();
                    txtthanhtien.Text = dt.Rows[i]["ThanhTien"].ToString();
                    txtthanhtien.Text = dt.Rows[i]["ThanhTienHang"].ToString();
                    string vat = dt.Rows[i]["CoVAT"].ToString();
                    
                    ckVAT.Checked = vat.Equals("") ? false : bool.Parse(dt.Rows[i]["CoVAT"].ToString());
                    
                    rowIndex++;
                }
            }
        }
    }
    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("NgayThang", typeof(string)));
      
        dt.Columns.Add(new DataColumn("NhaCungCap", typeof(string)));
       
        dt.Columns.Add(new DataColumn("DonVi", typeof(string)));
        dt.Columns.Add(new DataColumn("SoLuong", typeof(string)));
        dt.Columns.Add(new DataColumn("DonGia", typeof(string)));
        dt.Columns.Add(new DataColumn("ThanhTien", typeof(string)));
        dt.Columns.Add(new DataColumn("CoVAT", typeof(bool)));        
        dt.Columns.Add(new DataColumn("NhomVatLieu", typeof(string)));
        dt.Columns.Add(new DataColumn("MaThietBi", typeof(string)));
        dt.Columns.Add(new DataColumn("LoaiVatLieu", typeof(string)));
        dt.Columns.Add(new DataColumn("ThanhTienHang", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["NgayThang"] = string.Empty;
        dr["NhaCungCap"] = string.Empty;
        dr["LoaiVatLieu"] = string.Empty;
        dr["NhomVatLieu"] = string.Empty;
        dr["MaThietBi"] = string.Empty;     
        dr["DonVi"] = string.Empty;
        dr["SoLuong"] = string.Empty;
        dr["DonGia"] = string.Empty;
        dr["ThanhTien"] = string.Empty;
        dr["CoVAT"] = false;
        dr["ThanhTienHang"] = string.Empty;
        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;


        GV1.DataSource = dt;
        GV1.DataBind();
    }
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable"] = dt;
                GV1.DataSource = dt;
                GV1.DataBind();

                for (int i = 0; i < GV1.Rows.Count - 1; i++)
                {
                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }
    }
   
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
   
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {   
                for (int i = 0; i < GV1.Rows.Count; i++)
                {
                    TextBox txtngaythang = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtngaythang");
                    DropDownList dlnhacungcap = (DropDownList)GV1.Rows[i].Cells[2].FindControl("drnhacungcap");
                    DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[i].Cells[3].FindControl("drnhomvatlieu");
                    DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[i].Cells[4].FindControl("drloaivatlieu");
                    DropDownList dldonvi = (DropDownList)GV1.Rows[i].Cells[5].FindControl("dldonvi");
                    TextBox txtsoluong = (TextBox)GV1.Rows[i].Cells[6].FindControl("txtsoluong");
                    TextBox txtdongia = (TextBox)GV1.Rows[i].Cells[7].FindControl("txtdongia");
                    TextBox txtthanhtienhang = (TextBox)GV1.Rows[i].Cells[8].FindControl("txtthanhtienhang");
                    TextBox txtthanhtien = (TextBox)GV1.Rows[i].Cells[9].FindControl("txtthanhtien");
                    TextBox txtmathietbi = (TextBox)GV1.Rows[i].Cells[10].FindControl("txtmathietbi");
                    CheckBox ckVAT = (CheckBox)GV1.Rows[i].Cells[11].FindControl("ckVAT");
                        # region insert nhapkho
                    Guid kho = Guid.NewGuid();
                        p = new SqlParameter[15];
                        p[0] = new SqlParameter("@IDs",kho);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                        p[1].SqlDbType = SqlDbType.SmallDateTime;
                        p[2] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[3] = new SqlParameter("@NguoiMua",DBNull.Value);
                        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[4] = new SqlParameter("@NhomVatLieu", ex.GetGuid(dlnhomvatlieu.SelectedValue));
                        p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[5] = new SqlParameter("@LoaiVatLieu", ex.GetGuid(dlloaivatlieu.SelectedValue));
                        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[6] = new SqlParameter("@DonVi", ex.GetGuid(dldonvi.SelectedValue));
                        p[6].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[7] = new SqlParameter("@SoLuong", double.Parse(txtsoluong.Text.Trim().Equals("") ? "0" : txtsoluong.Text.Trim()));
                        p[7].SqlDbType = SqlDbType.Float;
                        p[8] = new SqlParameter("@DonGia", double.Parse(txtdongia.Text.Trim().Equals("") ? "0" : txtdongia.Text.Trim()));
                        p[8].SqlDbType = SqlDbType.Float;
                        p[9] = new SqlParameter("@ThanhTien", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                        p[9].SqlDbType = SqlDbType.Float;
                        p[10] = new SqlParameter("@MaThietBi", txtmathietbi.Text.Trim());
                        p[10].SqlDbType = SqlDbType.NVarChar;
                        p[11] = new SqlParameter("@Del", 1);
                        p[11].SqlDbType = SqlDbType.Int;
                        p[12] = new SqlParameter("@CoVAT", ckVAT.Checked ? 1 : 2);
                        p[12].SqlDbType = SqlDbType.Int;
                        p[13] = new SqlParameter("@IDThanhToan", DBNull.Value);
                        p[13].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[14] = new SqlParameter("@ThanhTienHang", double.Parse(txtthanhtienhang.Text.Trim().Equals("") ? "0" : txtthanhtienhang.Text.Trim()));
                        p[14].SqlDbType = SqlDbType.Float;
                        ex.ExecuteNonQuerry("sp_xdanhapkho_Add", p);
                        #endregion
                        Guid idtt = Guid.NewGuid();
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                        p[0].SqlDbType = SqlDbType.SmallDateTime;
                        dt = ex.GetData("sp_xdagetsophieuchiconnotravat_TienMat", p);
                            int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                            if (sopc == 0)
                                pc = 1;
                            else pc = sopc + 1;

                            p = new SqlParameter[21];
                            p[0] = new SqlParameter("@IDs", idtt);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[1] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                            p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[2] = new SqlParameter("@SoDuKiTruocVAT", double.Parse(0.ToString()));
                            p[2].SqlDbType = SqlDbType.Float;
                           
                            p[3] = new SqlParameter("@TienCuocVAT", double.Parse(0.ToString()));
                            p[3].SqlDbType = SqlDbType.Float;
                            p[4] = new SqlParameter("@SoDuKiTruocKhongVAT", double.Parse(0.ToString()));
                            p[4].SqlDbType = SqlDbType.Float;
                            
                            p[5] = new SqlParameter("@TienCuocKhongVAT", double.Parse(0.ToString()));
                            p[5].SqlDbType = SqlDbType.Float;
                            p[6] = new SqlParameter("@SoTienThanhToanTruocVAT", double.Parse(0.ToString()));
                            p[6].SqlDbType = SqlDbType.Float;
                            p[7] = new SqlParameter("@SoTienThanhToanKhongVAT", double.Parse(0.ToString()));
                            p[7].SqlDbType = SqlDbType.Float;
                            if (ckVAT.Checked == true)
                            {
                                p[8] = new SqlParameter("@TienHangVAT", double.Parse(txtthanhtienhang.Text.Trim()));
                                p[8].SqlDbType = SqlDbType.Float;
                                p[9] = new SqlParameter("@TienHangKhongVAT", double.Parse(0.ToString()));
                                p[9].SqlDbType = SqlDbType.Float;
                                p[10] = new SqlParameter("@SoTienThanhToanKiNayVAT", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                p[10].SqlDbType = SqlDbType.Float;
                                p[11] = new SqlParameter("@SoTienThanhToanKiNayKhongVAT", double.Parse(0.ToString()));
                                p[11].SqlDbType = SqlDbType.Float;
                            }
                            else
                            {
                                p[8] = new SqlParameter("@TienHangVAT", double.Parse(0.ToString()));
                                p[8].SqlDbType = SqlDbType.Float;
                                p[9] = new SqlParameter("@TienHangKhongVAT", double.Parse(txtthanhtienhang.Text.Trim()));
                                p[9].SqlDbType = SqlDbType.Float;
                                p[10] = new SqlParameter("@SoTienThanhToanKiNayVAT", double.Parse(0.ToString()));
                                p[10].SqlDbType = SqlDbType.Float;
                                p[11] = new SqlParameter("@SoTienThanhToanKiNayKhongVAT", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                p[11].SqlDbType = SqlDbType.Float;
                            }
                            p[12] = new SqlParameter("@HinhThucThanhToanVAT", 2);
                            p[12].SqlDbType = SqlDbType.Int;
                            p[13] = new SqlParameter("@HinhThucThanhToanKhongVAT", double.Parse(0.ToString()));
                            p[13].SqlDbType = SqlDbType.Int;
                            p[14] = new SqlParameter("@SoPC", pc);
                            p[14].SqlDbType = SqlDbType.Int;
                            p[15] = new SqlParameter("@TaiKhoanKhongVAT", DBNull.Value);
                            p[15].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[16] = new SqlParameter("@TaiKhoanVAT", DBNull.Value);
                            p[16].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[17] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                            p[17].SqlDbType = SqlDbType.SmallDateTime;
                            p[18] = new SqlParameter("@Del", 1);
                            p[18].SqlDbType = SqlDbType.Int;
                            p[19] = new SqlParameter("@NoiDung", "Thanh toán nhập kho");
                            p[19].SqlDbType = SqlDbType.NVarChar;
                            p[20] = new SqlParameter("@IDLenhChi", kho);
                            p[20].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_XDAThanhToanCongNoTra_Add", p);

                            p = new SqlParameter[11];
                            p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                            p[1].SqlDbType = SqlDbType.SmallDateTime;
                            p[2] = new SqlParameter("@NoiDung", "Thanh toán từ nhập kho");
                            p[2].SqlDbType = SqlDbType.NVarChar;
                            p[3] = new SqlParameter("@DonVi", ex.GetGuid(dlnhacungcap.SelectedValue));
                            p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[4] = new SqlParameter("@SoPC", pc);
                            p[4].SqlDbType = SqlDbType.Int;
                            p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                            p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[6] = new SqlParameter("@Del", 1);
                            p[6].SqlDbType = SqlDbType.Int;
                            p[7] = new SqlParameter("@LoaiCongNo", 2);
                            p[7].SqlDbType = SqlDbType.Int;
                            p[8] = new SqlParameter("@SoTien", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                            p[8].SqlDbType = SqlDbType.Float;
                            p[9] = new SqlParameter("@IDLenhChi", idtt);
                            p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[10] = new SqlParameter("@NhanVien", DBNull.Value);
                            p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Add", p);

                
                        
                }

                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thanh toán tiền mặt\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
   
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdanhapkho_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdanhapkho_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdanhapkho_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }


    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            txtTuNgay.Visible = true;
            txtDenNgay.Visible = true;
            dlNam.Visible = false;
        }
        else
        {
            txtTuNgay.Visible = false;
            txtDenNgay.Visible = false;
            dlNam.Visible = true;
        }
    }
    protected void btnXem_Click(object sender, EventArgs e)
    {
        getDate();
        if (dropNhanVien.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;



            GV.DataSource = ex.GetData("sp_xdanhapkho_List_TKTheoThoiGian", p);
            GV.DataBind();
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;

            GridView1.DataSource = ex.GetData("sp_xdanhapkho_List_TKTheoThoiGian", p);
            GridView1.DataBind();

        }
        else
        {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@NhaCungCap", ex.GetGuid(dropNhanVien.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GridView1.DataSource = ex.GetData("sp_xdanhapkho_List_TKTheoThoiGian_NCC", p);
            GridView1.DataBind();
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@NhaCungCap", ex.GetGuid(dropNhanVien.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GV.DataSource = ex.GetData("sp_xdanhapkho_List_TKTheoThoiGian_NCC", p);
            GV.DataBind();
        }
        btnFirst.Visible = false;
        btnPre.Visible = false; btnNext.Visible = false;

    }
    public void getDate()
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
        else
        {
            txtTuNgay.Text = "1/" + dlThang.SelectedValue + "/" + dlNam.SelectedValue;
            txtDenNgay.Text = GetLastDayOfMonth(int.Parse(dlThang.SelectedValue)).ToString("dd/MM/yyyy");
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
    }
    public DateTime GetLastDayOfMonth(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(dlNam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }

    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];


        if (e.CommandName.Equals("Del"))
        {
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelPhieuChiCongNo", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelThanhToanCongNoTra", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_xdanhapkho_Del", p);
            

            gstGetMess("Xóa thành công", "");
            btnCancel_Click(sender, e);
        }
    }

    protected void thanhtienhang_TextChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < GV1.Rows.Count; i++)
        {

            TextBox txtdongia = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtdongia");
            TextBox txtsoluong = (TextBox)GV1.Rows[i].Cells[2].FindControl("txtsoluong");
            TextBox txtthanhtien = (TextBox)GV1.Rows[i].Cells[3].FindControl("txtthanhtienhang");
            double thanhtien;
            thanhtien = double.Parse(txtdongia.Text.Trim()) * double.Parse(txtsoluong.Text.Trim());
            txtthanhtien.Text = thanhtien.ToString();
        }
    }



   
}